<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>爬虫状态及配置</title>
    <script src="https://cdn.staticfile.org/vue/2.6.7/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
    <script src="https://cdn.bootcss.com/element-ui/2.5.4/index.js"></script>
    <script src="js/common.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.5.4/theme-chalk/index.css"/>
    <link rel="icon" href="favicon.ico"/>
</head>
<body>
<h1>爬虫配置</h1>
<div id="app">
    <template>
        <el-table :data="tableData" border style="width: 100%" height="400">
            <el-table-column fixed prop="domain" label="作用域" width="150">
            </el-table-column>
            <el-table-column prop="userAgent" label="userAgent" width="300">
            </el-table-column>
            <el-table-column prop="charset" label="charset" width="120">
            </el-table-column>
            <el-table-column prop="sleepTime" label="sleepTime" width="120">
            </el-table-column>
            <el-table-column prop="retryTimes" label="retryTimes" width="100">
            </el-table-column>
            <el-table-column prop="cycleRetryTimes" label="cycleRetryTimes" width="130">
            </el-table-column>

            <el-table-column prop="retrySleepTime" label="retrySleepTime" width="120">
            </el-table-column>
            <el-table-column prop="timeOut" label="timeOut" width="120">
            </el-table-column>
            <el-table-column width="95" fixed="right">
                <template slot="header" slot-scope="scope">
                    <el-button type="primary" size="small" @click="dialogFormVisible = true">添加</el-button>
                </template>

                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">Delete
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>

    <el-dialog title="自定义配置" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="">
                <el-col class="line" :span="3">domain:</el-col>
                <el-col :span="8">
                    <el-input v-model="form.domain" placeholder="domain"></el-input>
                </el-col>
                <el-col class="line" :span="3" :offset="3">charset:</el-col>
                <el-col :span="7">
                    <el-input v-model="form.charset" placeholder="charset"></el-input>
                </el-col>
            </el-form-item>

            <el-form-item label="">
                <el-col class="line" :span="3">timeOut:</el-col>
                <el-col :span="7">
                    <el-input v-model="form.timeOut" placeholder="timeOut"></el-input>
                </el-col>

                <el-col class="line" :span="3" :offset="4">retryTimes:</el-col>
                <el-col :span="7">
                    <el-input v-model="form.retryTimes" placeholder="retryTimes"></el-input>
                </el-col>

            </el-form-item>

            <el-form-item label="">
                <el-col class="line" :span="3">sleepTime:</el-col>
                <el-col :span="7">
                    <el-input v-model="form.sleepTime" placeholder="sleepTime"></el-input>
                </el-col>

                <el-col class="line" :span="4" :offset="3">cycleRetryTimes:</el-col>
                <el-col :span="7">
                    <el-input v-model="form.cycleRetryTimes" placeholder="cycleRetryTimes"></el-input>
                </el-col>

            </el-form-item>

            <el-form-item label="">
                <el-col class="line" :span="4">userAgent:</el-col>
                <el-col :span="20">
                    <el-input v-model="form.userAgent" :span="7"></el-input>
                </el-col>

            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addConfig()">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>

    var v = new Vue({
        el: '#app',
        data: {
            dialogFormVisible: false,
            form: {
                domain: "",
                userAgent: "",
                charset: "",
                sleepTime: "",
                retryTimes: "",
                cycleRetryTimes: "",
                retrySleepTime: "",
                timeOut: ""
            },
            tableData: []

        },
        mounted: function () {
            this.fetchExampleConfig();
            this.fetchAllConfig();
        },
        methods: {
            fetchAllConfig() {
                this.$http.get(currentUrl(window) + CRAWLER_CONFIG_MAPPING + "?withId=true").then(function (response) {
                    if (response.status === 200) {
                        this.tableData = response.body.data;
                    }
                });
            },
            addConfig() {
                this.$http.post(currentUrl(window) + CRAWLER_CONFIG_MAPPING, JSON.stringify(this.form), {
                    headers: {
                        "Content-Type": "application/json; charset=UTF-8"
                    }
                }).then(function (response) {

                    if (response.status === 200) {
                        this.fetchAllConfig();
                    }
                });
                this.dialogFormVisible = false;
            },
            fetchExampleConfig() {
                this.$http.get(currentUrl(window) + DEFAULT_CRAWLER_CONFIG_MAPPING).then(function (response) {
                    if (response.status === 200) {
                        this.form = response.body.data;
                    }
                });
            },
            handleDelete(index, row) {
                this.$confirm('确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete(currentUrl(window) + CRAWLER_CONFIG_MAPPING + "?id=" + row.id).then(function (response) {
                        if (response.status === 200) {
                            this.tableData.splice(index, 1);
                        }
                    });
                }).catch(() => {
                });
            }

        }

    });

</script>
</body>
</html>